{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 普遍的投资方式是针对一只股票不断的进行买卖，他们不会长期持有一只股票，但是也不会离这只股票太远：\n",
    "#  原因1: 贪欲，自以为是；\n",
    "#  原因2:时间成本与懒惰"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd \n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['open', 'high', 'low', 'close', 'volume', 'pre_close', 'p_change',\n",
       "       'date', 'date_week', 'atr21', 'atr14', 'key'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tsla_df = pd.read_csv('./tsla_2.csv', parse_dates=True, index_col=0) \n",
    "tsla_df.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "tsla_df['positive'] = np.where(tsla_df.p_change > 0, 1, 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>close</th>\n",
       "      <th>volume</th>\n",
       "      <th>pre_close</th>\n",
       "      <th>p_change</th>\n",
       "      <th>date</th>\n",
       "      <th>date_week</th>\n",
       "      <th>atr21</th>\n",
       "      <th>atr14</th>\n",
       "      <th>key</th>\n",
       "      <th>positive</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2018-12-24</th>\n",
       "      <td>313.5</td>\n",
       "      <td>314.50</td>\n",
       "      <td>295.195</td>\n",
       "      <td>295.39</td>\n",
       "      <td>5559913</td>\n",
       "      <td>319.77</td>\n",
       "      <td>-7.624</td>\n",
       "      <td>20181224</td>\n",
       "      <td>0</td>\n",
       "      <td>18.793476</td>\n",
       "      <td>19.634311</td>\n",
       "      <td>314</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-12-26</th>\n",
       "      <td>300.0</td>\n",
       "      <td>326.97</td>\n",
       "      <td>294.090</td>\n",
       "      <td>326.09</td>\n",
       "      <td>8163138</td>\n",
       "      <td>295.39</td>\n",
       "      <td>10.393</td>\n",
       "      <td>20181226</td>\n",
       "      <td>2</td>\n",
       "      <td>20.074069</td>\n",
       "      <td>21.400403</td>\n",
       "      <td>315</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-12-27</th>\n",
       "      <td>300.0</td>\n",
       "      <td>326.97</td>\n",
       "      <td>294.090</td>\n",
       "      <td>326.09</td>\n",
       "      <td>8163138</td>\n",
       "      <td>326.09</td>\n",
       "      <td>0.000</td>\n",
       "      <td>20181227</td>\n",
       "      <td>3</td>\n",
       "      <td>21.238245</td>\n",
       "      <td>22.931016</td>\n",
       "      <td>316</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-12-28</th>\n",
       "      <td>300.0</td>\n",
       "      <td>326.97</td>\n",
       "      <td>294.090</td>\n",
       "      <td>326.09</td>\n",
       "      <td>8163138</td>\n",
       "      <td>326.09</td>\n",
       "      <td>0.000</td>\n",
       "      <td>20181228</td>\n",
       "      <td>4</td>\n",
       "      <td>22.296586</td>\n",
       "      <td>24.257547</td>\n",
       "      <td>317</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-12-31</th>\n",
       "      <td>300.0</td>\n",
       "      <td>326.97</td>\n",
       "      <td>294.090</td>\n",
       "      <td>326.09</td>\n",
       "      <td>8163138</td>\n",
       "      <td>326.09</td>\n",
       "      <td>0.000</td>\n",
       "      <td>20181231</td>\n",
       "      <td>0</td>\n",
       "      <td>23.258715</td>\n",
       "      <td>25.407207</td>\n",
       "      <td>318</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             open    high      low   close   volume  pre_close  p_change  \\\n",
       "2018-12-24  313.5  314.50  295.195  295.39  5559913     319.77    -7.624   \n",
       "2018-12-26  300.0  326.97  294.090  326.09  8163138     295.39    10.393   \n",
       "2018-12-27  300.0  326.97  294.090  326.09  8163138     326.09     0.000   \n",
       "2018-12-28  300.0  326.97  294.090  326.09  8163138     326.09     0.000   \n",
       "2018-12-31  300.0  326.97  294.090  326.09  8163138     326.09     0.000   \n",
       "\n",
       "                date  date_week      atr21      atr14  key  positive  \n",
       "2018-12-24  20181224          0  18.793476  19.634311  314         0  \n",
       "2018-12-26  20181226          2  20.074069  21.400403  315         1  \n",
       "2018-12-27  20181227          3  21.238245  22.931016  316         0  \n",
       "2018-12-28  20181228          4  22.296586  24.257547  317         0  \n",
       "2018-12-31  20181231          0  23.258715  25.407207  318         0  "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tsla_df.tail()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>positive</th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date_week</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>32</td>\n",
       "      <td>29</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>31</td>\n",
       "      <td>34</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>32</td>\n",
       "      <td>32</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>40</td>\n",
       "      <td>24</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>33</td>\n",
       "      <td>32</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "positive    0   1\n",
       "date_week        \n",
       "0          32  29\n",
       "1          31  34\n",
       "2          32  32\n",
       "3          40  24\n",
       "4          33  32"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用 pd.crosstab()构建一个交叉表， 行使用 date_week 列使用positive\n",
    "xt = pd.crosstab(tsla_df.date_week, tsla_df.positive)\n",
    "xt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>positive</th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date_week</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.524590</td>\n",
       "      <td>0.475410</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.476923</td>\n",
       "      <td>0.523077</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.500000</td>\n",
       "      <td>0.500000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.625000</td>\n",
       "      <td>0.375000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.507692</td>\n",
       "      <td>0.492308</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "positive          0         1\n",
       "date_week                    \n",
       "0          0.524590  0.475410\n",
       "1          0.476923  0.523077\n",
       "2          0.500000  0.500000\n",
       "3          0.625000  0.375000\n",
       "4          0.507692  0.492308"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 下面的代码经常和pd.crosstab() 配套出现的代码，其实就是求出各个所占的比例\n",
    "xt_pct = xt.div(xt.sum(1).astype(float), axis=0)\n",
    "xt_pct"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 大数定律， 统计套利的核心思想就是这个，不只是要单纯追求胜率，更应该关注大数定律，寻找多元化的交易机会\n",
    "# 最终达到理想的胜率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>positive</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date_week</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.475410</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.523077</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.500000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.375000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.492308</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           positive\n",
       "date_week          \n",
       "0          0.475410\n",
       "1          0.523077\n",
       "2          0.500000\n",
       "3          0.375000\n",
       "4          0.492308"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 透视表 上面的操作可以用更简单的工具 pivot_table() 透视表 求出结果 求出占比\n",
    "tsla_df.pivot_table(['positive'], index=['date_week'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "date_week  positive\n",
       "0          0           32\n",
       "           1           29\n",
       "1          0           31\n",
       "           1           34\n",
       "2          0           32\n",
       "           1           32\n",
       "3          0           40\n",
       "           1           24\n",
       "4          0           33\n",
       "           1           32\n",
       "Name: positive, dtype: int64"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# groupby() 函数 更底层，句法更难理解，可以解决的问题更加全面\n",
    "# 初学者 crosstab  pivot_table 求出对应比例\n",
    "tsla_df.groupby(['date_week','positive'])['positive'].count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
